<template>
    <div>
        <div class="permissManaTool">
            <el-input size="small" placeholder="请输入角色英文名" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input size="small" placeholder="请输入角色中文名" v-model="role.nameZh" @keydown.enter.native="doAddRole"></el-input>
            <el-button type="primary" size="small" icon="el-icon-plus" @click="doAddRole">添加角色</el-button>
        </div>
        <div  class="permissManaMain" >
            <el-collapse  accordion @change="change" v-model="activeName">
                <el-collapse-item  v-for="(r,index) in roles" :title="r.nameZh" :key="index"
                                    :name="r.id">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问的资源</span>
                            <el-button style="float: right; padding: 3px 0;color: #ff0000" icon="el-icon-delete" type="text"></el-button>
                        </div>
                        <div>
                            <el-tree :key="index" ref="tree" :data="allMenus" :props="defaultProps" show-checkbox node-key="id" :default-checked-keys="mids"></el-tree>
                            <div style="display: flex;justify-content: flex-end">
                                <el-button @click="cancelUpdate()">取消修改</el-button>
                                <el-button  type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissMana",
        data(){
            return {
                activeName:-1,
                role:{
                    name:'',
                    nameZh:''
                },
                roles:[],
                allMenus:[],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                mids:[]
            }
        },
        mounted(){
            this.initRoles();
        },
        methods:{
            doAddRole(){
                if (this.role.name && this.role.nameZh) {
                    this.putRequest("/system/basic/permiss/role", this.role).then(resp => {
                        if (resp) {
                            this.role.nameZh = "";
                            this.role.name = "";
                            this.initRoles();
                        }
                    });
                } else {
                    this.$message.error("数据不可以为空!")
                }
            },
            cancelUpdate(){
                this.activeName=-1;
            },
            doUpdate(rid,index){
                let tree = this.$refs.tree[index];
                let keys = tree.getCheckedKeys(true);
                let url = "/system/basic/permiss/?rid="+rid;
                keys.forEach(key=>{
                    url+="&mids="+key;
                })
                this.putRequest(url).then(resp=>{
                    if(resp){
                        this.activeName=-1;
                    }
                })

            },
            change(name){
                if(name){
                    this.initAllMenus();
                    this.initMids(name);
                };
            },
            initMids(name){
                this.getRequest("/system/basic/permiss/mids/"+name).then(resp=>{
                    if (resp){
                        this.mids=resp;
                    }
                })
            },
            initAllMenus(){
                this.getRequest("/system/basic/permiss/menus").then(resp=>{
                    if (resp){
                        this.allMenus=resp;
                    }
                })
            },
            initRoles(){
                this.getRequest("/system/basic/permiss/").then(resp=>{
                    console.log(resp)
                    this.roles=resp;
                })
            }
        }
    }
</script>

<style>
    .permissManaTool {
        display: flex;
        justify-content: flex-start;
    }
    .permissManaTool .el-input{
        width: 300px;
        margin-right: 6px;
    }
    .permissManaMain{
        margin-top: 10px;
        width:700px ;
    }
</style>